package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.StudentAllotTier;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.CommonUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class StudentAllotTierServiceImpl implements IStudentAllotTierService {

    @Autowired
    IStudentClassTypeService iStudentClassTypeService;

    @Autowired
    ITranscriptService iTranscriptService;

    @Autowired
    IStudentAllotTierSetService iStudentAllotTierSetService;

    @Autowired
    IStudentService iStudentService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IStudentGradeService iStudentGradeService;

    @Autowired
    IGradeService iGradeService;

    @Override
    public boolean studentClassTypeAdd(StudentAllotTier studentAllotTier) {

        Student student = iStudentService.getById(studentAllotTier.getStudentId());
        studentAllotTier.setYearClassId(student.getYearClassId());

        Long studentId = studentAllotTier.getStudentId();
        Long subjectsId = studentAllotTier.getSubjectsId();
        Integer yearPart = studentAllotTier.getYearPart();
        Integer quarterNum = studentAllotTier.getQuarterNum();

        QueryWrapper<StudentClassType> queryWrapper = new QueryWrapper<StudentClassType>();
        queryWrapper.eq("year_part", yearPart);
        queryWrapper.eq("quarter_num", quarterNum);
        queryWrapper.eq("student_id", studentId);
        queryWrapper.eq("subjects_id", subjectsId);
        queryWrapper.last("limit 1");
        StudentClassType studentClassType = iStudentClassTypeService.getOne(queryWrapper);

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(yearPart,quarterNum);
        //Integer examNum = 1;
        //期中
        Transcript midtermTranscript = iTranscriptService.getDataTranscript(yearPart,quarterNum,studentId,subjectsId,1);
        if(midtermTranscript != null){
            studentAllotTier.setMidtermScore(midtermTranscript.getScoreEnd());
        }else {
            studentAllotTier.setMidtermScore(new BigDecimal(0));
        }


        //期末
        Transcript endTermTranscript = iTranscriptService.getDataTranscript(yearPart,quarterNum,studentId,subjectsId,3);
        if(endTermTranscript != null){
            studentAllotTier.setEndTermScore(endTermTranscript.getScoreEnd());
        }else {
            studentAllotTier.setEndTermScore(new BigDecimal(0));
        }

        if (studentClassType == null) {
            studentClassType  = new StudentClassType();
            studentClassType.setYearPart(yearPart);
            studentClassType.setQuarterNum(quarterNum);
            studentClassType.setSubjectsId(subjectsId);
            studentClassType.setStudentId(studentId);
            studentClassType.setYearClassId(studentAllotTier.getYearClassId());
        }
        studentClassType.setPassShutQuarterId(quarter.getId());
        studentClassType.setRightRadeQuarterId(quarter.getId());
        studentClassType.setMidtermQuarterId(quarter.getId());
        studentClassType.setEndTermQuarterId(quarter.getId());

        studentClassType = this.getStudentClassType(studentAllotTier,studentClassType);
        iStudentClassTypeService.saveOrUpdate(studentClassType);

        return true;
    }

    public StudentClassType getStudentClassType(StudentAllotTier studentAllotTier,StudentClassType studentClassType) {
        Long classTypeId = 1575859909042028546L;//默认提高班
        String classTypeIdStr = "1575859909042028546";

        Long studentId = studentAllotTier.getStudentId();
        Long subjectsId = studentAllotTier.getSubjectsId();
        Integer yearPart = studentAllotTier.getYearPart();
        Integer quarterNum = studentAllotTier.getQuarterNum();

        Integer gradeSection = CommonUtil.getGradeSectionForYearClassId(studentAllotTier.getYearClassId());
        QueryWrapper<StudentAllotTierSet> studentAllotTierSetQueryWrapper = new QueryWrapper<>();
        studentAllotTierSetQueryWrapper.eq("year_part", yearPart);
        studentAllotTierSetQueryWrapper.eq("quarter_num", quarterNum);
        studentAllotTierSetQueryWrapper.eq("subjects_id", subjectsId);
        studentAllotTierSetQueryWrapper.and(i->i.eq("grade_section", gradeSection).or().eq("year_class_id",studentAllotTier.getYearClassId()));

        //studentAllotTierSetQueryWrapper.eq("grade_section", gradeSection);
        StudentAllotTierSet studentAllotTierSet = iStudentAllotTierSetService.getOne(studentAllotTierSetQueryWrapper);
        if (studentAllotTierSet!=null){
            if (studentAllotTierSet.getSubjectsId().equals(1L)){
                if (studentAllotTier.getPassShutNum()>=studentAllotTierSet.getPassShutNum()){
                    classTypeId = studentAllotTierSet.getClassTypeId();
                    classTypeIdStr = studentAllotTierSet.getClassTypeIdStr();
                }else if (studentAllotTierSet.getEndTermScore().compareTo(BigDecimal.ZERO) > 0 && studentAllotTier.getEndTermScore().compareTo(studentAllotTierSet.getEndTermScore())>=0){
                    classTypeId = studentAllotTierSet.getClassTypeId();
                    classTypeIdStr = studentAllotTierSet.getClassTypeIdStr();
                }else if (studentAllotTier.getMidtermScore().compareTo(BigDecimal.ZERO) > 0 && studentAllotTier.getMidtermScore().compareTo(studentAllotTierSet.getMidtermScore())>=0){
                    classTypeId = studentAllotTierSet.getClassTypeId();
                    classTypeIdStr = studentAllotTierSet.getClassTypeIdStr();
                }
            }else {
                if (studentAllotTier.getRightRade().compareTo(studentAllotTierSet.getRightRade())>=0){
                    classTypeId = studentAllotTierSet.getClassTypeId();
                    classTypeIdStr = studentAllotTierSet.getClassTypeIdStr();
                }else if (studentAllotTierSet.getEndTermScore().compareTo(BigDecimal.ZERO) > 0 && studentAllotTier.getEndTermScore().compareTo(studentAllotTierSet.getEndTermScore())>=0){
                    classTypeId = studentAllotTierSet.getClassTypeId();
                    classTypeIdStr = studentAllotTierSet.getClassTypeIdStr();
                }else if (studentAllotTier.getMidtermScore().compareTo(BigDecimal.ZERO) > 0 && studentAllotTier.getMidtermScore().compareTo(studentAllotTierSet.getMidtermScore())>=0){
                    classTypeId = studentAllotTierSet.getClassTypeId();
                    classTypeIdStr = studentAllotTierSet.getClassTypeIdStr();
                }
            }
        }
        if (studentClassType.getYearClassId() >= 10){
            studentClassType.setClassTypeIdStr("1575859909042028546,1565023758494502914");//高中都能添加
        }else {
            if (classTypeIdStr.equals("1575859909042028546")){
                //原来培优班不降到提高
                String classTypeAllot = this.getStudentClassTypeId(yearPart,quarterNum,subjectsId,studentId,1);
                if (classTypeAllot!=null){
                    classTypeIdStr = classTypeAllot;
                }
            }
            studentClassType.setClassTypeIdStr(classTypeIdStr);
        }

        studentClassType.setSubjectsId(subjectsId);
        studentClassType.setClassTypeId(classTypeId);
        studentClassType.setPassShutNum(studentAllotTier.getPassShutNum());
        studentClassType.setRightRade(studentAllotTier.getRightRade());
        studentClassType.setMidtermScore(studentAllotTier.getMidtermScore());
        studentClassType.setEndTermScore(studentAllotTier.getEndTermScore());
        return studentClassType;
    }

    public String getStudentClassTypeId(Integer yearPart,Integer quarterNum,Long subjectsId,Long studentId,Integer lessonType){
        QueryWrapper<StudentGrade> studentGradeQueryWrapper = new QueryWrapper<>();
        studentGradeQueryWrapper.eq("year_part", yearPart);
        studentGradeQueryWrapper.eq("quarter_num", quarterNum);
        studentGradeQueryWrapper.eq("subjects_id", subjectsId);
        studentGradeQueryWrapper.eq("student_id", studentId);
        studentGradeQueryWrapper.eq("lesson_type", lessonType);
        studentGradeQueryWrapper.eq("is_last", 1);
        studentGradeQueryWrapper.last("limit 1");
        StudentGrade studentGrade = iStudentGradeService.getOne(studentGradeQueryWrapper);
        String classTypeIdStr = null;
        if (studentGrade!=null){
            Grade grade = iGradeService.getById(studentGrade.getGradeId());
            if (grade!=null){
                classTypeIdStr = grade.getCourseId().toString();
            }
        }
        return classTypeIdStr;
    }


}
